﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.Sys.Orders
{
    public partial class Info : InfoPage
    {
        BLL.Orders bll = new BLL.Orders();
        Model.Orders model = new Model.Orders();
        //
        BLL.V_Orders bllV_Orders = new BLL.V_Orders();
        protected Model.V_Orders modelV_Orders = new Model.V_Orders();
        //
        BLL.V_Orders_Goods bllV_Orders_Goods = new BLL.V_Orders_Goods();
        BLL.Orders_Goods bllOrders_Goods = new BLL.Orders_Goods();
        //
        BLL.Store bllStore = new BLL.Store();
        //Model.Store modelStore = new Model.Store();
        //
        BLL.Orders_State_Log bllOrders_State_Log = new BLL.Orders_State_Log();
        //
        protected decimal ExpectPriceSum = 0, ActualPriceSum_Buying = 0, needMoreMoney = 0;
        protected EnumHelper.OrdersState OrderState;
        bool BeforeAndBuying = false, AfterAndBuying = false;
       
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {
            model = bll.GetModel(Master.id);
            modelV_Orders = bllV_Orders.GetModelList("id=" + Master.id)[0];

            OrderState = (EnumHelper.OrdersState)model.State;
            BeforeAndBuying = OrderState <= EnumHelper.OrdersState.Buying;
            AfterAndBuying = OrderState >= EnumHelper.OrdersState.Buying;


            Model.Store modelStore = Security.GetCurrentStore();
            ExpectPriceSum = BLL.Orders.Get_Orders_ExpectSellPriceSum(Master.id);
            needMoreMoney = ExpectPriceSum + 100 - (modelStore.Account - modelStore.FrozenAccount);
            if (needMoreMoney < 0) needMoreMoney = 0;
            needmoney.Text = StringPlus.showMoney_empty(needMoreMoney);
            if (Security.GetCurrentUserType() == EnumHelper.UserType.Store)
            {
                needmoney.Text = StringPlus.showMoney_empty(model.Getmoney);
                lblshowtxt.Text = "已打款：";
            }
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            if (Security.InUserType(new List<EnumHelper.UserType>() { EnumHelper.UserType.Buyer, EnumHelper.UserType.Supplier }))
            {//采购员+供货商 看不到：售价
                th_Price.Visible = false;
                td_ExpectPrice.Visible = false;
                td_ActualPrice.Visible = false;
                if (Security.GetCurrentUserType() == EnumHelper.UserType.Supplier)
                {//供应商 看不到：供货商名称

                }
            }
            if (Security.GetCurrentUserType() == EnumHelper.UserType.Store)
            {//孕婴店  看不到：进价
                th_BuyPrice.Visible = tb_ExpectBuyPrice.Visible = tb_ActualPrice.Visible = false;
                th_Pay.Visible = tb_ExpectPay.Visible = tb_ActualPay.Visible = false;
                //自己的名字
                tr_StoreName.Visible = false;
            }
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            rp_Orders_State_Log.DataSource = bllOrders_State_Log.GetModelList("OrdersID=" + Master.id+" order by id desc");
            rp_Orders_State_Log.DataBind();
            //
            //if (BBPA.Admin.Security.GetCurrentUserType() == EnumHelper.UserType.Buyer)
            //{
            //    th_Price.Visible = false;
            //    td_ExpectPrice.Visible = false;
            //    td_ActualPrice.Visible = false;
            //}
            //
            rp_Goods_Bing();
            //
            if (OrderState < EnumHelper.OrdersState.Buying)//采购前看不到 实际
            {
                rp_Goods.Controls[0].FindControl("th_ActualPrice").Visible = false;
                rp_Goods.Controls[0].FindControl("th_ActualAmount").Visible = false;
                rp_Goods.Controls[0].FindControl("th_ActualPriceSum").Visible = false;
                rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("td_Orders_ActualPrice").Visible = false;
            }
            if (OrderState > EnumHelper.OrdersState.Buying)//采购后看不到 预计
            {
                rp_Goods.Controls[0].FindControl("th_ExpectPrice").Visible = false;
                rp_Goods.Controls[0].FindControl("th_ExpectAmount").Visible = false;
                rp_Goods.Controls[0].FindControl("th_ExpectPriceSum").Visible = false;
                rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("td_Orders_ExpcetPriceSum").Visible = false;
            }
            //
            if (OrderState <= EnumHelper.OrdersState.Buying)//采购之前和采购时 能看到 预计合计
                ((Label)rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("lb_ExpectPriceSum")).Text = StringPlus.showMoney(ExpectPriceSum);
            if (OrderState >= EnumHelper.OrdersState.Buying)//采购时和采购之后 能看到 实际合计
            {
                decimal ActualPriceSum = model.ActualPriceSum;//
                if (OrderState == EnumHelper.OrdersState.Buying) ActualPriceSum = ActualPriceSum_Buying;
                ((Label)rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("lb_ActualPriceSum")).Text = StringPlus.showMoney(ActualPriceSum);
            }
        }

        void rp_Goods_Bing()
        {
            ExpectPriceSum = 0;
            rp_Goods.DataSource = bllV_Orders_Goods.GetModelList("OrdersID=" + Master.id + " order by IsDelivery desc,SupplierID,Dateandtime desc");
            rp_Goods.DataBind();
        }
        protected void rp_Goods_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Model.V_Orders_Goods rowData = (Model.V_Orders_Goods)e.Item.DataItem;
                //TextBox tb_ExpectAmount = (TextBox)e.Item.FindControl("tb_ExpectAmount");
                //tb_ExpectAmount.Text = rowData.ExpectAmount.ToString();
                //((TextBox)e.Item.FindControl("tb_id")).Text = rowData.id.ToString();
                //
                ExpectPriceSum += rowData.ExpectAmount * DataTypePlus.requestDecimal(rowData.SellPrice);
                ActualPriceSum_Buying += rowData.ActualAmount * DataTypePlus.requestDecimal(rowData.SellPrice);
                //
                if (OrderState < EnumHelper.OrdersState.Buying)
                {
                    e.Item.FindControl("td_ActualPrice").Visible = false;
                    e.Item.FindControl("td_ActualAmount").Visible = false;
                    e.Item.FindControl("td_ActualPriceSum").Visible = false;
                }
                if (OrderState > EnumHelper.OrdersState.Buying)
                {
                    e.Item.FindControl("td_ExpectPrice").Visible = false;
                    e.Item.FindControl("td_ExpectAmount").Visible = false;
                    e.Item.FindControl("td_ExpectPriceSum").Visible = false;
                }
            }

        }
    }
}